import Oidc from 'oidc-client'

const host = 'http://192.168.1.115:9528/'

const authsServer = 'https://localhost:5001'

const userManager = new Oidc.UserManager({
    authority: authsServer,  // server 地址
    client_id: 'client_manage',  // client id
    post_logout_redirect_uri: `${host}/logout`, // 退出登录
    redirect_uri: `${host}callback`,
    silent_redirect_uri: `${host}callback`,
    accessTokenExpiringNotificationTime: 4,  // 超时
    silentRequestTimeout: 2000,  // 
    response_type: 'id_token token',
    scope: 'openid profile',
    filterProtocolClaims: true
})

userManager.events.addUserSignedOut(() => {
    userManager.removeUser().then(() => {
        userManager.signinRedirect().catch(()=>{
            console.error('error wihile logout')
        })
    })

})

export default userManager;


// import { UserManager } from 'oidc-client'
// import Cookies from 'js-cookie'
// // import { ACCESS_TOKEN, USER_NAME, ID_TOKEN } from '@/store/mutation-types'

// const isProd = process.env.NODE_ENV === 'production'

// class ApplicationUserManager extends UserManager {
//   constructor () {
//     super({
//       authority: isProd ? globalConfig.Pro.AUTHORITY_URL : globalConfig.Dev.AUTHORITY_URL,
//       client_id: 'client_vue',
//       redirect_uri: isProd ? globalConfig.Pro.REDIRECT_URI : globalConfig.Dev.REDIRECT_URI,
//       response_type: 'id_token token',
//       scope: 'openid profile scope1 scope2',
//       post_logout_redirect_uri: isProd ? globalConfig.Pro.LOGOUT_REDIRECT_URI : globalConfig.Dev.LOGOUT_REDIRECT_URI,
//       silent_redirect_uri: isProd ? globalConfig.Pro.SILENT_REDIRECT_URI : globalConfig.Dev.SILENT_REDIRECT_URI,
//       automaticSilentRenew: true,
//       accessTokenExpiringNotificationTime: 30,
//       filterProtocolClaims: true,
//    })
//   }

//   async login () {
//     await this.signinRedirect()
//     return this.getUser()
//   }

//   async logout () {
//     console.log('调用 oidc-client 退出登陆')
//     // Cookies.remove(USER_NAME)
//     // Cookies.remove(ACCESS_TOKEN)
//     // window.localStorage.clear()
//     // const idToken = Cookies.get(ID_TOKEN)
//     // return this.signoutRedirect({ 'id_token_hint': idToken })
//   }
// }

// const applicationUserManager = new ApplicationUserManager()
// export { applicationUserManager as default }